<template>
  <div class="my-tag">
    <input
      v-focus
      v-if="isEdit"
      class="input"
      type="text"
      placeholder="输入标签"
      @blur="close"
      v-model="tag"
      @keyup.enter="close"
    />
    <div @dblclick="changeIsEdit" class="text" v-else>{{ value }}</div>
  </div>
</template>

<script>
export default {
  name: 'MyTag',
  props: {
    value: String,
  },
  data() {
    return {
      isEdit: false,
      tag: '',
    }
  },
  delimiters: {
    focus: {
      inserted(el) {
        el.focus()
      },
    },
  },
  methods: {
    changeIsEdit() {
      this.isEdit = !this.isEdit
      this.tag = this.value
    },
    close() {
      this.isEdit = false
      this.$emit('input', this.tag)
      if (!this.tag) {
        this.tag = '暂无'
      }
    },
  },
}
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
